extends Node2D
class_name Bullet

var _info:DamageInfo
var _speed=700
var _targetPos=Vector2()
var _regionId=1

func start(damageInfo):
	_info=damageInfo
	if !is_instance_valid(_info.to):
		return
	_regionId=_info.to.actorM.regionId
	_targetPos=_info.to.position
	pass
func _physics_process(delta):
	if is_instance_valid(_info.to):
		#更新位置 先对比一下前后位置的差距，如果太大了，相当于传送了，丢失目标
		var nowPos=_info.to.position
		var prePos=_targetPos
		if (nowPos-prePos).length()>500 || _regionId!=_info.to.actorM.regionId:
			_info.to=null #丢失目标
		else:
			_targetPos=nowPos #没有丢失目标，更新位置
	var dis=(_targetPos-position) as Vector2
	if dis.length()<=_speed*delta:
		position=_targetPos
		#打到了
		_hit()
	else:
		var velocity=dis.normalized()*_speed*delta
		position+=velocity
func _hit():
	if is_instance_valid(_info.to):
		BattleManager.damage(_info)
	dispose()
func dispose():
	queue_free()


